//
//  interfaceRunner.m
//  taptap-server
//
//  Created by Michael Humphris on 11/08/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "interfaceRunner.h"

@implementation interfaceRunner
@synthesize progressIndicator;
@synthesize btnServerStart,btnServerStop,btnUpdateThemes,btnUpdateTracks;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
        [DDLog addLogger:[DDTTYLogger sharedInstance]];
    }
    
    return self;
}

-(void)startServer:(id)sender{
    [btnServerStart setEnabled:NO];
    
    // Initalize our http server
	httpServer = [[HTTPServer alloc] init];
	
	// Tell the server to broadcast its presence via Bonjour.
	// This allows browsers such as Safari to automatically discover our service.
	[httpServer setType:@"_http._tcp."];
	
	// Normally there's no need to run our server on any specific port.
	// Technologies like Bonjour allow clients to dynamically discover the server's port at runtime.
	// However, for easy testing you may want force a certain port so you can just hit the refresh button.
    [httpServer setPort:8080];
	
    [httpServer setConnectionClass:[taptapHTTPConnection class]];
    
	// Serve files from the standard Sites folder
	NSString *docRoot = [@"~/Library/Application Support/TapTapServer" stringByExpandingTildeInPath];
	DDLogInfo(@"Setting document root: %@", docRoot);
	
	[httpServer setDocumentRoot:docRoot];
	
	NSError *error = nil;
	if(![httpServer start:&error])
	{
		DDLogError(@"Error starting HTTP Server: %@", error);
        [btnServerStart setEnabled:YES];
	}
    [btnServerStop setEnabled:YES];
}
-(void)stopServer:(id)sender{
    [httpServer stop];
    [httpServer release];
    [btnServerStart setEnabled:YES];
    [btnServerStop setEnabled:NO];
}

-(void)updateThemes:(id)sender{
    
    
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    [openDlg setCanChooseFiles:NO];
    [openDlg setCanChooseDirectories:YES];
    
    
    if ([openDlg runModal] == NSOKButton) {
        NSString* folder = [openDlg filename];
                
        [btnUpdateThemes setEnabled:NO];
        [btnUpdateTracks setEnabled:NO];
        
        ThemeManager* themeMgr = [[ThemeManager alloc] init];
        [themeMgr setDelegate:self];
        [themeMgr processDirectory:folder];
        [themeMgr release];
    }
    
}
-(void)updateTracks:(id)sender{    
    NSOpenPanel* openDlg = [NSOpenPanel openPanel];
    [openDlg setCanChooseFiles:NO];
    [openDlg setCanChooseDirectories:YES];
    
    
    if ([openDlg runModal] == NSOKButton) {
        NSString* folder = [openDlg filename];
        
        [btnUpdateThemes setEnabled:NO];
        [btnUpdateTracks setEnabled:NO];
        
        TrackManager* trackMgr = [[TrackManager alloc] init];
        [trackMgr setDelegate:self];
        [trackMgr processDirectory:folder];
        [trackMgr release];
    }
}

-(void)updateProgress:(float)progress sender:(id)sender{
    [progressIndicator setDoubleValue:(double)(progress)];
}
-(void)workDone:(id)sender{
    [btnUpdateThemes setEnabled:YES];
    [btnUpdateTracks setEnabled:YES];
}

-(void)dealloc{
    [self stopServer:nil];
}

@end
